package com.hy.controller;

import com.hy.base.BaseInfoProperties;
import com.hy.enums.YesOrNo;
import com.hy.grace.result.GraceJSONResult;
import com.hy.grace.result.ResponseStatusEnum;
import com.hy.pojo.bo.QuestionLibBO;
import com.hy.pojo.vo.InitQuestionsVO;
import com.hy.service.QuestionLibService;
import com.hy.utils.PagedGridResult;
import com.hy.utils.RedisOperator;
import io.micrometer.common.util.StringUtils;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/questionLib")
public class QuestionLibController extends BaseInfoProperties {

    @Autowired
    private QuestionLibService questionLibService;


    /**
     * 创建或更新题库
     * @param questionLibBO
     * @return
     */
    @PostMapping("/createOrUpdate")
    // @Valid 校验面试官信息
    public GraceJSONResult createOrUpdate(@Valid @RequestBody QuestionLibBO questionLibBO) {
        questionLibService.createOrUpdate(questionLibBO);
        return GraceJSONResult.ok();
    }

    /**
     * 查询题库列表
     * @return
     */
    @GetMapping("/list")
    public GraceJSONResult list(@RequestParam String aiName,
                                @RequestParam String question,
                                @RequestParam(defaultValue = "1",name = "page") Integer page,
                                @RequestParam(defaultValue = "10",name = "pageSize") Integer pageSize) {
        PagedGridResult result = questionLibService.queryList(aiName, question, page, pageSize);
        return GraceJSONResult.ok(result);
    }

    /**
     * 启用当前题目
     * @param questionLibId
     * @return
     */
    @PostMapping("/show")
    public GraceJSONResult show(@RequestParam String questionLibId) {
        if (StringUtils.isBlank(questionLibId)) {
            return GraceJSONResult.error();
        }
        questionLibService.setDisplayOrNot(questionLibId, YesOrNo.YES.type);
        return GraceJSONResult.ok();
    }

    /**
     * 禁用当前题目
     * @param questionLibId
     * @return
     */
    @PostMapping("/hide")
    public GraceJSONResult hide(@RequestParam String questionLibId) {
        if (StringUtils.isBlank(questionLibId)){
            return GraceJSONResult.error();
        }
        questionLibService.setDisplayOrNot(questionLibId, YesOrNo.NO.type);
        return GraceJSONResult.ok();
    }

    /**
     * 删除当前题目
     * @param questionLibId
     * @return
     */
    @PostMapping("/delete")
    public GraceJSONResult delete(@RequestParam String questionLibId) {
        if (StringUtils.isBlank(questionLibId)){
            return GraceJSONResult.error();
        }
        questionLibService.delete(questionLibId);
        return GraceJSONResult.ok();
    }

    /**
     * 准备面试题，随机获得一定数量的面试题返回给前端
     * @param candidateId
     * @return
     */
    @GetMapping("/prepareQuestion")
    public GraceJSONResult prepareQuestion(@RequestParam String candidateId) {

        // 从Redis中获取候选人信息和用户令牌，验证会话是否有效，限制接口被恶意调用
        String candidateInfo = redis.get(REDIS_USER_INFO + ":" + candidateId);
        String userToken = redis.get(REDIS_USER_TOKEN + ":" + candidateId);

        // 如果候选人信息或用户令牌为空，说明会话已失效或不存在，返回错误信息
        if (StringUtils.isBlank(candidateInfo) || StringUtils.isBlank(userToken)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_INFO_NOT_EXIST_ERROR);
        }

        // 调用服务获取随机面试题
        List<InitQuestionsVO> result = questionLibService.getRandomQuestions(candidateId, 3);
        return GraceJSONResult.ok(result);

    }
}
